package internalMail;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpringLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import com.sun.rowset.CachedRowSetImpl;
import flexibility.CachedRowSetTableModel;
import flexibility.userData;
import basic.ClientConsole;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;

public class createMailBox {

	public static JFrame mailScreen;
	public JTable table;
	public JTextPane textPane_1;
	private getStudents GS = new getStudents();
	private ClientConsole con;
	private CachedRowSetImpl crs;
	private userData mail = new userData();
	private CachedRowSetTableModel data;
	public JTextArea textArea;
	private int userID;
	public JLabel lblYouHave;
	private JTextArea textArea_1;
	private JButton btnSendReplay;
	private int replyTO = -1;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnAll;
	private JLabel lblYourReply;
	private JLabel lblContactSelected;
	private JRadioButton rdbtnClass;
	private int state = -1;
	private int semesterID;
	private String recipient;

	/**
	 * @wbp.parser.entryPoint
	 */
	public createMailBox() {

		// this will create the window
		mailScreen = new JFrame("Academic Information System - Notifications");
		mailScreen.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				mailScreen.dispose();
			}
		});
		mailScreen.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		mailScreen.setSize(889, 501);
		mailScreen.move(100, 100);
		mailScreen.setResizable(false);
		// mailScreen.setVisible(true);
		SpringLayout springLayout = new SpringLayout();
		mailScreen.getContentPane().setLayout(springLayout);

		table = new JTable();

		springLayout.putConstraint(SpringLayout.NORTH, table, 69,
				SpringLayout.NORTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, table, -63,
				SpringLayout.EAST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, table, 283,
				SpringLayout.NORTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, table, -1050,
				SpringLayout.EAST, mailScreen.getContentPane());
		table.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null }, }, new String[] {
				"New column", "New column", "New column" }));

		JScrollPane scrollPane = new JScrollPane(table);
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 10,
				SpringLayout.WEST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -40,
				SpringLayout.SOUTH, mailScreen.getContentPane());
		mailScreen.getContentPane().add(scrollPane);
		// mailScreen.getContentPane().add(textPane);

		JLabel lblMessageContent = new JLabel("Message content:");
		mailScreen.getContentPane().add(lblMessageContent);

		lblYourReply = new JLabel("Your reply: ");
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, -6,
				SpringLayout.WEST, lblYourReply);
		springLayout.putConstraint(SpringLayout.WEST, lblYourReply, 0,
				SpringLayout.WEST, lblMessageContent);
		mailScreen.getContentPane().add(lblYourReply);

		btnSendReplay = new JButton("Send ");
		btnSendReplay.setEnabled(false);
		mailScreen.getContentPane().add(btnSendReplay);

		JButton btnApprove = new JButton("Approve");

		springLayout.putConstraint(SpringLayout.WEST, btnApprove, 695,
				SpringLayout.WEST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnSendReplay, -40,
				SpringLayout.WEST, btnApprove);
		springLayout.putConstraint(SpringLayout.NORTH, btnApprove, 0,
				SpringLayout.NORTH, btnSendReplay);
		mailScreen.getContentPane().add(btnApprove);

		JButton btnDeny = new JButton("Deny");
		btnDeny.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (replyTO != -1) {
					// this will get the replier name
					mail.deleteAll();
					mail.addData(335);
					mail.addData(1);
					mail.addData(userID);
					con.acceptMsg(mail);
					crs = con.getResults();
					String sender = null;
					try {
						crs.next();
						sender = crs.getString(1) + " " + crs.getString(2);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// this will get the recipient name
					// this will get the replier name
					String recipient = null;
					if (state == 0) {

						mail.deleteAll();
						mail.addData(340);
						mail.addData(1);
						mail.addData(replyTO);
						con.acceptMsg(mail);
						crs = con.getResults();
						try {
							crs.next();
							recipient = crs.getString(1) + " "
									+ crs.getString(2);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else {
						mail.addData(339);
						mail.addData(1);
						mail.addData(replyTO);
						con.acceptMsg(mail);
						crs = con.getResults();
						try {
							crs.next();
							recipient = crs.getString(1) + " "
									+ crs.getString(2);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					mail.deleteAll();
					mail.deleteAll();
					mail.addData(417);
					mail.addData(0);
					mail.addData(replyTO);
					mail.addData(userID);
					mail.addData(1);
					Calendar reportTime = Calendar.getInstance();
					SimpleDateFormat formatter = new SimpleDateFormat(
							"yyyy/MM/dd");
					mail.addData(formatter.format(reportTime.getTime()));
					mail.addData(sender);
					mail.addData("Denied!");
					mail.addData(recipient);
					mail.addData(0);
					con.acceptMsg(mail);
					crs = con.getResults();
					JOptionPane.showMessageDialog(mailScreen,
							"Your reply was sent!",
							"Send message - Confirmation",
							JOptionPane.INFORMATION_MESSAGE);
					replyTO = -1;
				}
			}
		});
		springLayout.putConstraint(SpringLayout.EAST, btnApprove, -6,
				SpringLayout.WEST, btnDeny);
		springLayout.putConstraint(SpringLayout.WEST, btnDeny, 787,
				SpringLayout.WEST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnDeny, -10,
				SpringLayout.EAST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, btnDeny, 0,
				SpringLayout.NORTH, btnSendReplay);
		mailScreen.getContentPane().add(btnDeny);

		JButton btnNewMessage = new JButton("New message");
		springLayout.putConstraint(SpringLayout.WEST, btnSendReplay, 6,
				SpringLayout.EAST, btnNewMessage);
		springLayout.putConstraint(SpringLayout.WEST, btnNewMessage, 0,
				SpringLayout.WEST, lblMessageContent);
		mailScreen.getContentPane().add(btnNewMessage);

		JLabel lblMessageTasks = new JLabel("Message tasks:");
		springLayout.putConstraint(SpringLayout.NORTH, btnNewMessage, 6,
				SpringLayout.SOUTH, lblMessageTasks);
		springLayout.putConstraint(SpringLayout.SOUTH, lblMessageTasks, -116,
				SpringLayout.SOUTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblMessageTasks, 0,
				SpringLayout.WEST, lblMessageContent);
		mailScreen.getContentPane().add(lblMessageTasks);

		JLabel lblQuickTasks = new JLabel("Quick tasks:");
		springLayout.putConstraint(SpringLayout.NORTH, lblQuickTasks, 0,
				SpringLayout.NORTH, lblMessageTasks);
		springLayout.putConstraint(SpringLayout.EAST, lblQuickTasks, -128,
				SpringLayout.EAST, mailScreen.getContentPane());
		mailScreen.getContentPane().add(lblQuickTasks);

		textArea_1 = new JTextArea();
		springLayout.putConstraint(SpringLayout.NORTH, textArea_1, 218,
				SpringLayout.NORTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, textArea_1, 6,
				SpringLayout.EAST, scrollPane);
		springLayout.putConstraint(SpringLayout.SOUTH, textArea_1, -6,
				SpringLayout.NORTH, lblMessageTasks);
		springLayout.putConstraint(SpringLayout.EAST, textArea_1, -31,
				SpringLayout.EAST, mailScreen.getContentPane());
		// mailScreen.getContentPane().add(textArea_1);

		JScrollPane sp2 = new JScrollPane(textArea_1,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		springLayout.putConstraint(SpringLayout.WEST, sp2, 6,
				SpringLayout.EAST, scrollPane);
		springLayout.putConstraint(SpringLayout.EAST, sp2, -10,
				SpringLayout.EAST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, btnSendReplay, 26,
				SpringLayout.SOUTH, sp2);
		springLayout.putConstraint(SpringLayout.SOUTH, sp2, -136,
				SpringLayout.SOUTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, sp2, 200,
				SpringLayout.NORTH, mailScreen.getContentPane());
		mailScreen.getContentPane().add(sp2);

		textArea = new JTextArea();
		textArea.setEditable(false);
		springLayout.putConstraint(SpringLayout.NORTH, textArea, 6,
				SpringLayout.SOUTH, lblMessageContent);
		springLayout.putConstraint(SpringLayout.WEST, textArea, 6,
				SpringLayout.EAST, scrollPane);
		springLayout.putConstraint(SpringLayout.SOUTH, textArea, -10,
				SpringLayout.NORTH, lblYourReply);
		springLayout.putConstraint(SpringLayout.EAST, textArea, -10,
				SpringLayout.EAST, mailScreen.getContentPane());
		// mailScreen.getContentPane().add(textArea);

		JScrollPane sp3 = new JScrollPane(textArea,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		springLayout.putConstraint(SpringLayout.WEST, sp3, 6,
				SpringLayout.EAST, scrollPane);
		springLayout.putConstraint(SpringLayout.SOUTH, sp3, -299,
				SpringLayout.SOUTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, sp3, -10,
				SpringLayout.EAST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, lblYourReply, 6,
				SpringLayout.SOUTH, sp3);
		springLayout.putConstraint(SpringLayout.NORTH, sp3, 37,
				SpringLayout.NORTH, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblMessageContent, -6,
				SpringLayout.NORTH, sp3);
		mailScreen.getContentPane().add(sp3);

		final JRadioButton rdbtnStaff = new JRadioButton("Staff");
		rdbtnStaff.setEnabled(false);
		mailScreen.getContentPane().add(rdbtnStaff);

		final JRadioButton rdbtnStudents = new JRadioButton("Students");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnStudents, 0,
				SpringLayout.NORTH, rdbtnStaff);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnStudents, 6,
				SpringLayout.EAST, rdbtnStaff);
		rdbtnStudents.setEnabled(false);
		mailScreen.getContentPane().add(rdbtnStudents);

		JButton btnRecieveNewMail = new JButton("Recieve new");
		springLayout.putConstraint(SpringLayout.NORTH, btnRecieveNewMail, 6,
				SpringLayout.SOUTH, scrollPane);
		springLayout.putConstraint(SpringLayout.WEST, btnRecieveNewMail, 0,
				SpringLayout.WEST, scrollPane);
		springLayout.putConstraint(SpringLayout.EAST, btnRecieveNewMail, -736,
				SpringLayout.EAST, mailScreen.getContentPane());
		btnRecieveNewMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblContactSelected.setVisible(false);
				state = 1;
				textArea.setEnabled(true);
				textArea.setBackground(Color.WHITE);
				lblYourReply.setText("Your reply:");
				rdbtnStaff.setEnabled(false);
				rdbtnNewRadioButton.setEnabled(false);
				rdbtnClass.setEnabled(false);
				rdbtnStudents.setEnabled(false);
				rdbtnAll.setEnabled(false);
				mail.deleteAll();
				mail.addData(327);
				mail.addData(1);
				mail.addData(userID);
				con.acceptMsg(mail);
				crs = con.getResults();
				try {
					int i = 0;// number of messages
					crs.beforeFirst();
					while (crs.next()) {
						i++;
					}
					if (i == 0) {
						btnSendReplay.setEnabled(false);
					}
					lblYouHave.setText("You have " + i + " new messages");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				updateTable();
			}
		});
		mailScreen.getContentPane().add(btnRecieveNewMail);

		JButton btnRecieveAllMail = new JButton("All inbox");
		springLayout.putConstraint(SpringLayout.NORTH, btnRecieveAllMail, 6,
				SpringLayout.SOUTH, scrollPane);
		springLayout.putConstraint(SpringLayout.WEST, btnRecieveAllMail, 6,
				SpringLayout.EAST, btnRecieveNewMail);
		btnRecieveAllMail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lblContactSelected.setVisible(false);
				state = 1;
				textArea.setEnabled(true);
				textArea.setBackground(Color.WHITE);
				lblYourReply.setText("Your reply:");
				rdbtnStaff.setEnabled(false);
				rdbtnNewRadioButton.setEnabled(false);
				btnSendReplay.setEnabled(false);
				rdbtnStudents.setEnabled(false);
				rdbtnClass.setEnabled(false);
				rdbtnAll.setEnabled(false);
				mail.deleteAll();
				mail.addData(331);
				mail.addData(1);
				mail.addData(userID);
				con.acceptMsg(mail);
				crs = con.getResults();
				try {
					int i = 0;// number of messages
					crs.beforeFirst();
					while (crs.next()) {
						i++;
					}
					if (i == 0) {
						btnSendReplay.setEnabled(false);
					}
					lblYouHave.setText("You have " + i + " messages in inbox");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				updateTable();
			}
		});
		mailScreen.getContentPane().add(btnRecieveAllMail);

		lblYouHave = new JLabel("You have ");
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 6,
				SpringLayout.SOUTH, lblYouHave);
		springLayout.putConstraint(SpringLayout.WEST, lblYouHave, 10,
				SpringLayout.WEST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblYouHave, -524,
				SpringLayout.EAST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblMessageContent, 86,
				SpringLayout.EAST, lblYouHave);
		springLayout.putConstraint(SpringLayout.SOUTH, lblYouHave, -445,
				SpringLayout.SOUTH, mailScreen.getContentPane());
		lblYouHave.setForeground(Color.BLUE);
		mailScreen.getContentPane().add(lblYouHave);

		rdbtnNewRadioButton = new JRadioButton("Parents");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnNewRadioButton, 0,
				SpringLayout.NORTH, rdbtnStaff);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnNewRadioButton, 4,
				SpringLayout.EAST, rdbtnStudents);
		rdbtnNewRadioButton.setEnabled(false);
		mailScreen.getContentPane().add(rdbtnNewRadioButton);

		rdbtnAll = new JRadioButton("All");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnStaff, 0,
				SpringLayout.NORTH, rdbtnAll);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnStaff, 6,
				SpringLayout.EAST, rdbtnAll);

		rdbtnAll.setSelected(true);
		rdbtnAll.setEnabled(false);
		mailScreen.getContentPane().add(rdbtnAll);

		rdbtnClass = new JRadioButton("Course");

		rdbtnClass.setEnabled(false);
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnClass, 0,
				SpringLayout.NORTH, rdbtnStaff);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnClass, 6,
				SpringLayout.EAST, rdbtnNewRadioButton);
		mailScreen.getContentPane().add(rdbtnClass);

		ButtonGroup bg = new ButtonGroup();
		bg.add(rdbtnStaff);
		bg.add(rdbtnStudents);
		bg.add(rdbtnNewRadioButton);
		bg.add(rdbtnAll);
		bg.add(rdbtnClass);

		JLabel lblContacts = new JLabel("Contacts: ");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnAll, -4,
				SpringLayout.NORTH, lblContacts);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnAll, 6,
				SpringLayout.EAST, lblContacts);
		springLayout.putConstraint(SpringLayout.NORTH, lblContacts, 6,
				SpringLayout.SOUTH, btnNewMessage);
		springLayout.putConstraint(SpringLayout.WEST, lblContacts, 0,
				SpringLayout.WEST, lblMessageContent);
		mailScreen.getContentPane().add(lblContacts);

		JButton btnRecieveAllSent = new JButton("Sent items");
		springLayout.putConstraint(SpringLayout.WEST, btnRecieveAllSent, 296,
				SpringLayout.WEST, mailScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnRecieveAllMail, -6,
				SpringLayout.WEST, btnRecieveAllSent);
		springLayout.putConstraint(SpringLayout.NORTH, btnRecieveAllSent, 6,
				SpringLayout.SOUTH, scrollPane);
		springLayout.putConstraint(SpringLayout.EAST, btnRecieveAllSent, 0,
				SpringLayout.EAST, scrollPane);
		mailScreen.getContentPane().add(btnRecieveAllSent);

		lblContactSelected = new JLabel("Contact selected ");
		lblContactSelected.setVisible(false);
		lblContactSelected.setForeground(Color.RED);
		springLayout.putConstraint(SpringLayout.NORTH, lblContactSelected, 6,
				SpringLayout.SOUTH, lblContacts);
		springLayout.putConstraint(SpringLayout.WEST, lblContactSelected, 6,
				SpringLayout.EAST, scrollPane);
		mailScreen.getContentPane().add(lblContactSelected);

		/* action for click on a message */
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				replyTO = Integer.parseInt(table.getValueAt(
						table.rowAtPoint(e.getPoint()), 0).toString());
				System.out.println("reply=" + replyTO);
				lblContactSelected.setVisible(true);
				if (state == 1) {
					btnSendReplay.setEnabled(true);
					mail.deleteAll();
					mail.addData(332);
					mail.addData(1);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();
					try {
						crs.next();
						textArea.setText(crs.getString(1));
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					mail.deleteAll();
					mail.addData(333);
					mail.addData(0);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();

					mail.deleteAll();
					mail.addData(339);
					mail.addData(1);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();
					try {
						crs.next();
						recipient = crs.getString(1);
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if (state == 2) {
					mail.deleteAll();
					mail.addData(332);
					mail.addData(1);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();
					try {
						crs.next();
						textArea.setText(crs.getString(1));
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					mail.deleteAll();
					mail.addData(333);
					mail.addData(0);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();
				}
				if (state == 3) {
					// send to all class

				}
				if (state == 0) {
					System.out.println("hihihihihihihihih");
					mail.deleteAll();
					mail.addData(337);
					mail.addData(1);
					mail.addData(table.getValueAt(
							table.rowAtPoint(e.getPoint()), 0));
					con.acceptMsg(mail);
					crs = con.getResults();
					try {
						crs.next();
						recipient = crs.getString(1) + crs.getString(2);
						System.out.println("recipient" + recipient);
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		/* new message was clicked */
		btnNewMessage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lblContactSelected.setVisible(false);
				state = 0; // state is on new message
				replyTO = -1;
				textArea_1.setText("");
				textArea.setEnabled(false);
				textArea.setBackground(Color.LIGHT_GRAY);
				lblYourReply.setText("Write your message here:");
				btnSendReplay.setEnabled(true);
				rdbtnStaff.setEnabled(true);
				rdbtnClass.setEnabled(true);
				rdbtnNewRadioButton.setEnabled(true);
				rdbtnStudents.setEnabled(true);
				lblYouHave.setText("All users contact list:");
				mail.deleteAll();
				mail.addData(336);
				mail.addData(1);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
				rdbtnAll.setEnabled(true);
				rdbtnAll.setSelected(true);
			}
		});
		/* students radio button was clicked */
		rdbtnStudents.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblYouHave.setText("Students contact list:");
				mail.deleteAll();
				mail.addData(329);
				mail.addData(1);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
			}
		});

		/* case class was selected */
		rdbtnClass.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblYouHave.setText("Courses contact list:");
				state = 3;// this is the send to course state
				mail.deleteAll();
				mail.addData(401);
				mail.addData(1);
				mail.addData(userID);
				mail.addData(semesterID);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
				// 401 gives the courses the teacher teaches
				// 402 gives all the students that learn the course
			}
		});

		/* cas all radio button was clicked */
		rdbtnAll.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblYouHave.setText("All users contact list:");
				mail.deleteAll();
				mail.addData(336);
				mail.addData(1);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
			}
		});

		/* case when parents radio button was clicked */
		rdbtnNewRadioButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblYouHave.setText("Parents contact list:");
				mail.deleteAll();
				mail.addData(330);
				mail.addData(1);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
			}
		});
		/* action for send button */
		btnSendReplay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lblContactSelected.setVisible(false);
				if (textArea_1.getText().equals("")) {
					JOptionPane.showMessageDialog(mailScreen,
							"You can not send empty reply!",
							"Send message - failurer",
							JOptionPane.ERROR_MESSAGE);
				}
				if (replyTO == -1) {
					JOptionPane.showMessageDialog(mailScreen,
							"Please select contact person from contact list",
							"Send message - failurer",
							JOptionPane.ERROR_MESSAGE);
				}
				if (replyTO != -1 && textArea_1.getText().equals("") == false) {
					// this will get the replier name
					mail.deleteAll();
					mail.addData(335);
					mail.addData(1);
					mail.addData(userID);
					con.acceptMsg(mail);
					crs = con.getResults();
					String sender = null;
					try {
						crs.next();
						sender = crs.getString(1) + " " + crs.getString(2);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// this will get the recipient name
					// this will get the replier name
					// recipient = null;
					if (state == 0) {
						// this is for new message
						mail.deleteAll();
						mail.addData(334);
						mail.addData(0);
						mail.addData(replyTO);
						mail.addData(userID);
						mail.addData(1);
						Calendar reportTime = Calendar.getInstance();
						SimpleDateFormat formatter = new SimpleDateFormat(
								"yyyy/MM/dd");
						mail.addData(formatter.format(reportTime.getTime()));
						mail.addData(sender);
						mail.addData(textArea_1.getText());
						mail.addData(recipient);
						con.acceptMsg(mail);
						crs = con.getResults();
						JOptionPane.showMessageDialog(mailScreen,
								"Your message was sent!",
								"Send message - Confirmation",
								JOptionPane.INFORMATION_MESSAGE);
						replyTO = -1;

					}
					if (state == 1) {
						mail.deleteAll();
						mail.addData(334);
						mail.addData(0);
						mail.addData(replyTO);
						mail.addData(userID);
						mail.addData(1);
						Calendar reportTime = Calendar.getInstance();
						SimpleDateFormat formatter = new SimpleDateFormat(
								"yyyy/MM/dd");
						mail.addData(formatter.format(reportTime.getTime()));
						mail.addData(sender);
						mail.addData(textArea_1.getText());
						mail.addData(recipient);
						con.acceptMsg(mail);
						crs = con.getResults();
						JOptionPane.showMessageDialog(mailScreen,
								"Your reply was sent!",
								"Send message - Confirmation",
								JOptionPane.INFORMATION_MESSAGE);
						replyTO = -1;
					}

				}
			}
		});
		/* case whae sent mails button is pressed */
		btnRecieveAllSent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lblContactSelected.setVisible(false);
				btnSendReplay.setEnabled(false);
				state = 2;
				lblYouHave.setText("Your sent mail:");
				mail.deleteAll();
				mail.addData(338);
				mail.addData(1);
				mail.addData(userID);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
			}
		});

		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (replyTO != -1) {
					// this will get the replier name
					mail.deleteAll();
					mail.addData(335);
					mail.addData(1);
					mail.addData(userID);
					con.acceptMsg(mail);
					crs = con.getResults();
					String sender = null;
					try {
						crs.next();
						sender = crs.getString(1) + " " + crs.getString(2);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// this will get the recipient name
					// this will get the replier name
					String recipient = null;
					if (state == 0) {

						mail.deleteAll();
						mail.addData(340);
						mail.addData(1);
						mail.addData(replyTO);
						con.acceptMsg(mail);
						crs = con.getResults();
						try {
							crs.next();
							recipient = crs.getString(1) + " "
									+ crs.getString(2);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else {
						mail.addData(339);
						mail.addData(1);
						mail.addData(replyTO);
						con.acceptMsg(mail);
						crs = con.getResults();
						System.out.println("why why why");
						try {
							crs.next();
							recipient = crs.getString(1) + " "
									+ crs.getString(2);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					mail.deleteAll();
					mail.deleteAll();
					mail.addData(417);
					mail.addData(0);
					mail.addData(replyTO);
					mail.addData(userID);
					mail.addData(1);
					Calendar reportTime = Calendar.getInstance();
					SimpleDateFormat formatter = new SimpleDateFormat(
							"yyyy/MM/dd");
					mail.addData(formatter.format(reportTime.getTime()));
					mail.addData(sender);
					mail.addData("Approved!");
					mail.addData(recipient);
					mail.addData(1);
					con.acceptMsg(mail);
					crs = con.getResults();
					JOptionPane.showMessageDialog(mailScreen,
							"Your reply was sent!",
							"Send message - Confirmation",
							JOptionPane.INFORMATION_MESSAGE);
					replyTO = -1;
				}
			}
		});

		/* case that staff radio button was clicked */
		rdbtnStaff.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				lblYouHave.setText("Staff contact list:");
				mail.deleteAll();
				mail.addData(328);
				mail.addData(1);
				con.acceptMsg(mail);
				crs = con.getResults();
				updateTable();
			}
		});
	}

	public void updateTable() {
		try {
			// if(ssc.getSemesterForEdit()==1){
			table.setModel(data = new CachedRowSetTableModel(crs));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			for (int i = 0; i < table.getColumnCount(); i++)
				table.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);

			// csw.table.setEnabled(false);
			// }

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setConnection(ClientConsole conn) {
		con = conn;
	}

	public void setJDBC(CachedRowSetImpl crss) {
		crs = crss;
	}

	public void setUserID(int ID) {
		userID = ID;
	}

	public void setSemesterID(int id) {
		semesterID = id;
	}
}
